#ifndef __ENGINE_REQUEST_CONNECT_included
#define __ENGINE_REQUEST_CONNECT_included

#include "Request.h"
#include "Device.h"
#include "proxy.h"

namespace Engine {

class CRequestConnect : public CRequest {

public :

	CRequestConnect(boost::asio::streambuf& data) :
		CRequest(string(COMMAND_CONNECT), data)
	{
	}

	CRequestConnect(string& server) :
		CRequest(string(COMMAND_CONNECT))
	{ 
		AddParameter(string("server"), server);
	}

	virtual void DoRequest() { 
		
		int peer = GetParameterInt(string("peer"));
		if(peer == 0) {
			Response(RESPONSE_STATUS_BadRequest);
			return;
		}

		ResponseRef response = InitResponse();
		string server = "proxy://" + mDevice->GetLocalIP() + ':' + boost::lexical_cast<string>(PROXY_PORT) + 
						"?peer=" + boost::lexical_cast<string>(peer);
		JsonBuffer ss;
		JsonWriter writer(ss);
		writer.StartObject();
		writer.String("server").String(server.c_str());
		writer.EndObject();

		response->SetContent(string(ss.GetString()), string(PACKAGE_CONTENT_TYPE_JSON));
		response->SetStatus(RESPONSE_STATUS_OK);
		mDevice->SendPackage(response);
	}
};

}

#endif //__ENGINE_REQUEST_CONNECT_included